﻿//------------------------------------------------------------------------------------------------------
// The MIT License (MIT) Copyright (c) 2014 Marton Rusko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using AutoMapper;

namespace RM.Banking.Unicredit.Statements
{
    /// <summary>
    /// Class to process Unicredit Statement files (STM)
    /// </summary>
    internal class UnicreditStatementFileProcessor
    {
        #region Private fields

        /// <summary>
        /// Data Records
        /// </summary>
        private readonly UnicreditStatementHeaderRecord header;

        /// <summary>
        /// Data Records
        /// </summary>
        private readonly List<UnicreditStatementDataRecord> rows = new List<UnicreditStatementDataRecord>();

        #endregion

        #region Public properties
        
        /// <summary>
        /// Header Record
        /// </summary>
        public UnicreditStatementHeaderRecord Header
        {
            get { return header; }
        }

        /// <summary>
        /// Data Records
        /// </summary>
        public IEnumerable<UnicreditStatementDataRecord> Rows
        {
            get { return rows; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initialize a UnicreditStatementFileProcessor
        /// </summary>
        /// <param name="data">Byte data of the statement file</param>
        public UnicreditStatementFileProcessor(byte[] data)
        {
            using (var reader = new StreamReader(new MemoryStream(data)))
            {
                UnicreditStatementRecord record;

                while ((record = UnicreditStatementRecordFactory.Create(reader)) != null)
                {
                    if (record is UnicreditStatementHeaderRecord)
                    {
                        header = record as UnicreditStatementHeaderRecord;
                        continue;
                    }

                    var dataRecord = record as UnicreditStatementDataRecord;
                    if(dataRecord == null)
                        throw new Exception("Invalid record type.");

                    rows.Add(dataRecord);
                }
            }
        }

        #endregion
    }
}
